package cn.yitun.chat.yitunservercommonlib.dataentity.vo.system;

import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class RouterVo implements Serializable {
    private static final long serialVersionUID = 1L;

    private String path;
    private String component;
    private String id;
    private String name;

    private String redirect;
    /**
     * 是否为叶子节点
     */
    private boolean leaf;
    private boolean hidden;
    private String parentId;
    private String iconCls;
    List<RouterVo> children;

    public static List<RouterVo>
    buildList(List<RouterVo> nodes, String idParam) {
        if (nodes == null) {
            return null;
        }
        List<RouterVo> topNodes = new ArrayList<>();
        for (RouterVo child : nodes) {
            String pid = child.getParentId();
            if (YitunStringUtil.isNotEmpty(pid)  || idParam.equals(pid) ) {
                topNodes.add(child);
                continue;
            }
            for (RouterVo parent : nodes) {
                String id = parent.getId();
                if ( YitunStringUtil.isNotEmpty(id)  && id.equals(pid)) {
                    parent.getChildren().add(child);
                    // child.setHasParent(true);
                    // parent.setChildren(true);
//                    parent.setLeaf(false);
                    continue;
                } else {
//                     parent.setLeaf(true);
                }
            }
        }
        return topNodes;
    }
}
